<?php
require_once '../../../wp-blog-header.php';
//wp_create_user('nikkhil.narang+test', 'buzzr', 'nikkhil.narang+test@enthrallmedia.com');


require_once 'inc/MCAPI.class.php';
require_once(ABSPATH . WPINC . '/registration.php');

$api_pod = new PodAPI();

$daily_cat = new Pod('category');
$daily_cat->page = 1;
$daily_cat->search = false; 
$daily_cat->findRecords('id ASC', -1, "is_local =1 OR is_daily=1");
while($daily_cat->fetchRecord())
{
  $cat_prefs[] = $daily_cat->get_field('id');
}
$cat_prefs = implode(',',$cat_prefs);

$api = new MCAPI2('bdfdc9236a45ff03c3533c5a2d3ae9c7-us1');
$retval = $api->listMembers('9cc36c69ee', 'subscribed', null, 0, 5000);

unset($emails);
foreach($retval['data'] as $k=>$member){
    $test = $api->listMemberInfo('9cc36c69ee', array($member['email']));
    $city = $test['data'][0]['merges']['GROUPINGS'][0]['groups'];
    $emails[$k]['city'] = $city; 
    $emails[$k]['email'] = $member['email'];
}
print_r($emails);

unset($active);
unset($has_pod);
unset($has_not_pod);
unset($inactive);
foreach($emails as $val)
{
  unset($email_id);
  $email_id = $val['email'];
  echo "<pre>";
  //print_r($email_id);
  print_r($val);
  if(email_exists($email_id) > 0)
  {
    //echo $val.'-->'.email_exists($email).'<br />';
    $active[] = $email_id;
    $del_pod = new Pod('users');
    $del_pod->page = 1;
    $del_pod->search = false; 
    $del_pod->findRecords('id DESC', 1, "wp_user.user_email='$email_id'");
    if($del_pod->getTotalRows() > 0)
    {
      $city_name = $val['city'];
      $daily_city = new Pod('city');
      $daily_city->page = 1;
      $daily_city->search = false; 
      $daily_city->findRecords("id ASC", -1, "t.name='$city_name'");
      while($daily_city->fetchRecord())
      {
        $city_id = $daily_city->get_field('id');
      }
      
      while($del_pod->fetchRecord())
      {  
         $user_pod_id = $del_pod->get_field('id');
         $has_pod[] = $email_id;
         $columns_user['daily_newsletter'] = 1;
         $columns_user['city'] = $city_id;
         $columns_user['categories'] = $cat_prefs;
         $params = array('datatype' => 'users','tbl_row_id' => $user_pod_id,'columns' => $columns_user);
         echo "<pre>";
         print_r($params);
         $api_pod->save_pod_item($params);
         //echo $val.'-->-->has pod<br /><br />';
      }
    }
    else
    {
      $has_not_pod[] = email_exists($email_id);
    }
  }
  else
  {
    //echo $val.'--> NULL<br />';
    $inactive[] = $email_id;
  }
}

echo 'Registered Users:'.count($active).'<br />';
echo 'Users with pods:'.count($has_pod).'<br />';
echo 'Users without pods:'.count($has_not_pod).'<br />';
echo 'Normal Users:'.count($inactive).'<br />';

//echo "<pre>";
//print_r($emails);

?>